//index.js
var app = getApp()
// 语言
var util = require('../../../utils/util.js')
import event from '../../../utils/event'

Page({
  data: {
    alreadyGoods:[],
    categories: [],
    indicatorDots: true,
    autoplay: true,
    interval: 8000,
    duration: 800,
    swiperCurrent: 0,
    selectCurrent: 0,
    activeCategoryId: 0,
    nonehidden: true,
    searchidden: true,
    height:980,
    TwoCategoryId:null,
    threeLists:[],
    threeActive:null,
    page:1,
    search:"",
    goodsLists:[],
    isContain:false
  },
  // inputblur
  inputBlur(e){
    this.setData({
      search:e.detail.value,
      page:1,
      goodsLists:[]
    })
    this.getGoodsLists()
  },
  // 三级分类
  changeThree(e){
    let item =  e.currentTarget.dataset.item;
    this.setData({
      threeActive:item.id,
      page:1,
      goodsLists:[],
    })
    this.getGoodsLists()
  },
  // 二级分类
  childCatege(e){
    let item =  e.currentTarget.dataset.item;
    this.setData({
      TwoCategoryId:item.id,
      threeLists:item.children,
      page:1,
      threeActive:item.children[0].id,
      goodsLists:[],
    })
    this.getGoodsLists()
  },
  tabClick: function (e) {
    let dataset= e.currentTarget.dataset
    this.setData({
      activeCategoryId:dataset.index,
      TwoCategoryId:dataset.item.children[0].id,
      threeLists:dataset.item.children[0].children,
      threeActive:dataset.item.children[0].children[0].id,
    });
  },
  onLoad: function () {
    this.initCategory();
    // 获取系统信息
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        // 获取可使用窗口宽度
        let clientHeight = res.windowHeight-res.statusBarHeight;
        // 获取可使用窗口高度
        let clientWidth = res.windowWidth;
        // 算出比例
        let ratio = 750 / clientWidth;
        // 算出高度(单位rpx)
        let height = clientHeight * ratio;
        // 设置高度
        that.setData({
          height: height-210-110
        });
      }
    })
  },
  // 获取商品列表
  getGoodsLists(){
    var that = this
    wx.request({
      url: app.globalData.urls + '/product/product-material',
      header: app.getRequestHeader(),
      method: 'GET',
      data: {
        bdmin_user_id:util.getBdmin_user_id(),
        page:this.data.page,
        ...(this.data.search ? {product_name:this.data.search} : {
          category_id:this.data.threeActive
        }),
        order_field:'created_at'//时间created_at 销量sale_num
      },
      success: function (res) {
        if (res.data.code == 200) {
          let data=res.data.data,getData = that.data.goodsLists.concat(data),
          alreadyGoods = that.data.alreadyGoods;
          let getRelate = alreadyGoods.reduce((prev,val)=>{
            prev[val.id]=val;
            return prev;
          },{})
          
          getData.forEach(val=>{
            if(getRelate[val.id])  val.hide = true
          })
          that.setData({
            goodsLists: getData,
            isContain:data.length==0
          })
        }

      }
    })
  },
  // 我也卖
  mysale(e){
    let item=e.currentTarget.dataset;
    this.data.alreadyGoods.push(item.item)
    this.setData({
      alreadyGoods:this.data.alreadyGoods,
      ['goodsLists['+item.index+'].hide'] :true
    })
  },
  // 删除所选的
  deleteGoods(e){
    let item=e.currentTarget.dataset;
    let alreadyGoods = this.data.alreadyGoods.filter(val=>val.id!=item.item.id)
    this.setData({
      alreadyGoods:alreadyGoods,
      ['goodsLists['+item.index+'].hide'] :false
    })
  },
  // 确认添加
  confrim(){
    var that = this,ids= this.data.alreadyGoods.map(val=>val.id);
    
    if(ids.length>0){
      wx.request({
        url: app.globalData.urls + '/product/product-material-add',
        header: app.getRequestHeader(),
        method: 'POST',
        data: {
          bdmin_user_id:util.getBdmin_user_id(),
          product_ids:ids.join(',')
        },
        success: function (res) {
          if (res.data.code == 200) {
            wx.showToast({
              title: '添加成功',
            })
            that.setData({
              alreadyGoods:[]
            })
          }else{
            wx.showToast({
              title: res.data.message,
              icon:"none"
            })
          }
  
        }
      })
    }
  },
  // 分类
  initCategory: function () {
    var that = this
    wx.request({
      url: app.globalData.urls + '/category/list',
      header: app.getRequestHeader(),
      data:{
        bdmin_user_id:util.getBdmin_user_id()
      },
      success: function (res) {
        if (res.data.code == 200) {
          let getData = res.data.data,two=getData[0].children[0];
          wx.hideLoading();
          that.setData({
            categories: getData,
            activeCategoryId: 0,
            TwoCategoryId:two.id,
            threeLists:two.children,
            threeActive:two.children[0].id
          });
          that.getGoodsLists();

        }

      }
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  bindscrolltolower: function () {
    if(!this.isContain){
      this.setData({
        page:this.data.page+1
      })
      this.getGoodsLists()
    }
  },
  // 全选
  allSelected(){
    let goodsLists = this.data.goodsLists.map(val=>{
      if(val.is_sale!=1) val.hide = !this.data.isSelect;
     
      return val;
    })
    
    this.setData({
      isSelect:!this.data.isSelect,
      alreadyGoods:goodsLists.filter(val=>val.hide),
      goodsLists:goodsLists
    })
  }
})